package com.zjdiepu.www.biz.dz;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dz.DzLoanMiddleBankMapper;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleBankService;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleBank;
import com.zjdiepu.www.model.dz.ext.DzLoanMiddleBankExt;
@Service
public class DzLoanMiddleBankServiceImpl extends BaseService implements IDzLoanMiddleBankService{
	public static final Logger logger = LoggerFactory.getLogger(DzLoanMiddleBankServiceImpl.class);

	@Autowired
	private DzLoanMiddleBankMapper dzLoanMiddleBankMapper;
	
	@Override
	public void saveUpdateAndDelete(DzLoanMiddleBank dzLoanMiddleBank, OperateType operType) {
		switch (operType) {
		case add:
			dzLoanMiddleBank.initInsertData(dzLoanMiddleBank.getModifier(), dzLoanMiddleBank.getModifierName());
			dzLoanMiddleBankMapper.insert(dzLoanMiddleBank);
			break;
		case edit:
			dzLoanMiddleBank.initUpdateData(dzLoanMiddleBank.getModifier(), dzLoanMiddleBank.getModifierName());
			dzLoanMiddleBank.setDpStatus(1);
			// 影响的记录数
			int updateCount = dzLoanMiddleBankMapper.updateByPrimaryKey(dzLoanMiddleBank);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dzLoanMiddleBank.initDeleteData(dzLoanMiddleBank.getModifier(), dzLoanMiddleBank.getModifierName());
			// 影响的记录数
			int deleteCount = dzLoanMiddleBankMapper.updateByPrimaryKeySelective(dzLoanMiddleBank);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DzLoanMiddleBank> selectByExt(DzLoanMiddleBankExt dzLoanMiddleBankExt) {
		return dzLoanMiddleBankMapper.selectByExt(dzLoanMiddleBankExt);
	}
	
	@Override
	public int countByExt(DzLoanMiddleBankExt dzLoanMiddleBankExt){
		return dzLoanMiddleBankMapper.countByExt(dzLoanMiddleBankExt);
	}
	
	@Override
	public DzLoanMiddleBank selectById(String id) {
		return dzLoanMiddleBankMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dzLoanMiddleBankMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DzLoanMiddleBank dzLoanMiddleBank) {
		return dzLoanMiddleBankMapper.countRefined(dzLoanMiddleBank);
	}

	@Override
	public DzLoanMiddleBank selectRefined(DzLoanMiddleBank dzLoanMiddleBank) {
		return dzLoanMiddleBankMapper.selectRefined(dzLoanMiddleBank);
	}

	@Override
	public boolean repeat(DzLoanMiddleBank dzLoanMiddleBank) {
		int count = countRefined(dzLoanMiddleBank);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dzLoanMiddleBank.getId())) {
			return true;
		}
		DzLoanMiddleBank dbItem = selectRefined(dzLoanMiddleBank);
		if(null == dbItem) {
			return false;
		}
		return !dzLoanMiddleBank.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dzLoanMiddleBankMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public void batchUpdate(DzLoanMiddleBank dzLoanMiddleBank) {
		dzLoanMiddleBankMapper.batchUpdate(dzLoanMiddleBank);
		
	}

	@Override
	public void batchInsert(List<DzLoanMiddleBank> listDhOverdue) {
		dzLoanMiddleBankMapper.batchInsert(listDhOverdue);
		
	}

	@Override
	public void batchDelete(DzLoanMiddleBank dzLoanMiddleBank) {
		dzLoanMiddleBankMapper.batchDelete(dzLoanMiddleBank);
		
	}

	@Override
	public DzLoanMiddleBank selectMuiltList(
			DzLoanMiddleBankExt dzLoanMiddleBankExt) {
		return dzLoanMiddleBankMapper.selectMuiltList(dzLoanMiddleBankExt);
	}
}
